package com.wall.haven.util;

import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.http.MediaType;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 修改Response返回工具类
 *
 * @author ChenWangKun
 * @date 2023-06-08
 **/

public class ResponseUtil {

    /**
     * 写入返回结果
     *
     * @param response  请求返回
     * @param apiResult 返回结果
     * @author ChenWangKun
     * @date 2023/6/8
     */
    public static void result(HttpServletResponse response, R<Object> apiResult) throws IOException {
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        ServletOutputStream out = response.getOutputStream();
        out.write(JsonUtils.toJson(apiResult).getBytes(StandardCharsets.UTF_8));
        out.flush();
        out.close();
    }
}
